package common.project.epic.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class EpicOpenHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "epicLocalDb";
	private static final int DB_VERSION = 1;
	
	public EpicOpenHelper(Context context){
		
		super(context, DB_NAME, null, DB_VERSION);
		
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		//tabella dei settaggi di Epic
		//TODO TABELLA CHE MEMORIZZI I SETTINGS DELL'APP
		
		//tabella dei contenuti
		String createContent = "CREATE TABLE content (id TEXT PRIMARY KEY NOT NULL , origin_mac TEXT NOT NULL , lat REAL , long REAL, span INTEGER, life TEXT NOT NULL, rank INTEGER NOT NULL, uri TEXT, content TEXT, id_category INTEGER NOT NULL, id_type INTEGER NOT NULL hop_count INTEGER, replication_counter INTEGER);";
		db.execSQL(createContent);
		
		//tabella delle categorie
		String createCategory = "CREATE TABLE category (cat_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , description TEXT NOT NULL );		";
		db.execSQL(createCategory);
		
		
		//tabella destinata alla memorizzazione dei diversi profili
		String createEpicsProfile = "CREATE TABLE epics_profile (id_profile INTEGER PRIMARY KEY, id_type INTEGER, name TEXT, surname TEXT, sex BOOLEAN, mail TEXT, password TEXT, uri TEXT, description TEXT, ranking INTEGER);";
		db.execSQL(createEpicsProfile);
		
		
		//tabella con la lista degli interessi
		String interests_list = "CREATE TABLE interests_list (interests_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , description VARCHAR);";
		db.execSQL(interests_list);
		//inserimento degli interessi
		insertinterests(db);
		
		//interessi relativi ad ogni utente
		String createInterests = "CREATE TABLE interests (interests_id INTEGER NOT NULL , profile_id INTEGER NOT NULL );";
		db.execSQL(createInterests);
	
		//interessi appartenenti al messaggio
		String createContent2Interests = "CREATE TABLE contoint (interests_id INTEGER NOT NULL , content_id INTEGER NOT NULL );";
		db.execSQL(createContent2Interests);
		
		//lista dei tipi di messaggio (Ex: business gold, business platinum etc... )
		String type_list = "CREATE TABLE type_list (type_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , name VARCHAR NOT NULL);";
		db.execSQL(type_list);
		//inserimento dei tipi
		inserttypes(db);
		
		//lista delle categorie di messaggio (Ex: eventi, mostre, party, flash mob...)
		String category_list = "CREATE TABLE category_list (category_id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , name VARCHAR NOT NULL);";
		db.execSQL(category_list);
		//inserimento delle categorie
		insertcategories(db);
		
		//tabelle dei dispositivi
		//tabella contenente la black list dei dispositivi bannati
		String deviceBL = "CREATE TABLE deviceBL (mac TEXT PRIMARY KEY NOT NULL  UNIQUE , name VARCHAR );";
		db.execSQL(deviceBL);
		//tabella contenente gli ultimi dispositivi contattati
		String deviceHY = "CREATE TABLE deviceHY (mac TEXT PRIMARY KEY NOT NULL  UNIQUE , name VARCHAR , lastBumpDate TEXT NOT NULL , msgCounter INTEGER NOT NULL );";
		db.execSQL(deviceHY);
		//tabella contenente i dispositivi scoperti e disponibili a comunicare, con possibilit� di ordinarli
		String deviceRT = "CREATE TABLE deviceRT (mac TEXT PRIMARY KEY NOT NULL  UNIQUE , name VARCHAR , rssi STRING , priority INTEGER );";
		db.execSQL(deviceRT);
		//table that track last and current locations
		String locationRT = "CREATE TABLE locationRT (id TEXT PRIMARY KEY NOT NULL , long REAL NOT NULL , lat REAL NOT NULL);";
		db.execSQL(locationRT);
		
	}

	private void insertinterests(SQLiteDatabase db) {
		String[] categoriesname = {"intA", "intB", "intC", "intD", "intE"};
		
		for(String element:categoriesname){
			ContentValues types = new ContentValues();
			types.put("name", element);
			db.insert("interests_list", null, types);
			
		}
		
	}

	private void inserttypes(SQLiteDatabase db) {
		// inserisce i tipi dall'array typesname nella tabella
			
		String[] typesname = {"tipoA", "tipoB", "tipoC", "tipoD", "tipoE"};
	
		for(String element:typesname){
			ContentValues types = new ContentValues();
			types.put("name", element);
			db.insert("type_list", null, types);
			
		}
		
	}
	
	private void insertcategories(SQLiteDatabase db) {
		// inserisce le categorie dell'array categoriesname nella tabella
			
		String[] categoriesname = {"catA", "catB", "catC", "catD", "catE"};
	
		for(String element:categoriesname){
			ContentValues types = new ContentValues();
			types.put("name", element);
			db.insert("category_list", null, types);
			
		}
		
	}
	

	@Override
	public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
		// TODO da modificare ogni qualvolta viene rilasciato un aggiornamento che prevede una modifica sul database
		
	}

}
